package com.tianji.aigc.config;

import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import com.tianji.aigc.memory.RedisChatMemory;
import com.tianji.aigc.properties.SessionProperties;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * AIGC配置类
 *
 * @Author mr.wu
 * @Date 2025-9-2 15:57
 */
@Configuration
public class AIGCConfig {

    @Autowired
    private SessionProperties sessionProperties;


    /**
     * 声明ChatClient对话实例
     * @param dashScopeChatModel
     * @return
     */
    @Bean
    public ChatClient chatClient(DashScopeChatModel dashScopeChatModel, MessageChatMemoryAdvisor messageChatMemoryAdvisor) {
        return ChatClient
                .builder(dashScopeChatModel)//模型对象
                .defaultSystem(sessionProperties.getSystem())//默认的系统提示词
                .defaultAdvisors(new SimpleLoggerAdvisor(), //日志记录器
                        messageChatMemoryAdvisor)//会话记忆记录器
                .build();
    }

    /**
     * 声明会话记忆的记录器
     * @param redisChatMemory
     * @return
     */
    @Bean
    public MessageChatMemoryAdvisor messageChatMemoryAdvisor(RedisChatMemory redisChatMemory) {
        return new MessageChatMemoryAdvisor(redisChatMemory);
    }
}
